package com.doit.day04

import com.doit.day03.ScalaOrder

object _04_边缘算子 {
  def main(args: Array[String]): Unit = {
    val arr: Array[Int] = Array(1, 3, 2, 5, 4, 6)
    val maybeInt: Option[Int] = arr.find(_ % 2 == 1)
    if (maybeInt.isDefined) {
      println(maybeInt.get)
    }


    val orders: Array[ScalaOrder] = Array(
      ScalaOrder("o001", "g002", 9.9, 10),
      ScalaOrder("o002", "g001", 29.9, 29),
      ScalaOrder("o003", "g005", 29.9, 4),
      ScalaOrder("o004", "g004", 22.9, 2),
      ScalaOrder("o005", "g003", 29.9, 1)
    )

    //按照什么属性去取最大值
    //ScalaOrder("o002", "g001", 29.9, 29),
    val order1: ScalaOrder = orders.maxBy(order=>{(-order.goodsNum,order.price)})
    println(order1)


    //如果集合里面的元素是可比较的，那么你就可以调用sorted 直接排序
    //如果不是一个可比较的，那么就只能传一个比较器
    /*    orders.sorted(new Ordering[ScalaOrder] {
          override def compare(x: ScalaOrder, y: ScalaOrder) = x.gid.compare(y.gid)
        })
        //排序算子  sorted   sortBy  sortWith
        val sorted: Array[Int] = arr.sorted
        println(sorted.mkString(","))*/

    //元祖天生就是可比较的 比较规则是先比较第一个元素，在比较第二个元素
    //sortBy 值得是针对一个对象中的一个或者多个属性去比较
    val orders1: Array[ScalaOrder] = orders.sortBy((order: ScalaOrder) => {
      (-order.price, order.gid)
    })

    val list: List[String] = List("Toy Story (1995)", "Guardian Angel (1994)", "Belle de jour (1967)", "New York Cop (1996)")
    list.sortBy(str => {
      str.substring(str.length - 5, str.length - 1)
    }).foreach(println)



    list.sortWith((str1, str2) => {
      str1.substring(str1.length - 5, str1.length - 1).compare(str2.substring(str2.length - 5, str2.length - 1)) > 0
    })
      .foreach(println)


    //聚合算子
    //reduce 叫规约 聚合
    val list1: List[Int] = List(1, 2, 3, 4, 5)
    val i: Int = list1.reduce((a, b) => a - b)
    println(i)


    val j: Int = list1.reduceRight((a, b) => a - b)
    println(j)//3

    //fold 折叠  默认初始值
    println(list1.fold(10)((a, b) => a + b))//25
    println(list1.fold(10)((a, b) => a - b))//-5
    println(list1.foldRight(10)((a, b) => a - b))//-7   10 先给b


    //zip 拉链
    val ints: List[Int] = List(1, 2, 3, 4, 5)
    val ints1: List[Int] = List(10, 20, 30, 40)
    val tuples: List[(Int, Int)] = ints.zip(ints1)
    println(tuples)//List((1,10), (2,20), (3,30), (4,40), (5,50))

    //zipWithIndex
    val index: List[(Int, Int)] = ints.zipWithIndex

    println(index)


    //slice
    val ints2: List[Int] = ints.slice(1, 3)
    println(ints2)



    //sliding   滑动窗口
    val iterator: Iterator[List[Int]] = ints.sliding(3, 2)
    while (iterator.hasNext){
      println(iterator.next())
    }





  }

}
